﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GomeeSoft.src.classes;
using GomeeSoft.src.classes.DiaryPages;

namespace GomeeSoft.src.form.DiaryPages
{
    public partial class UCMonthL : UserControl
    {
        // Varibles -----=======================================================================
        #region Varibles
        private int year = DateTime.Now.Year;
        public int Year
        {
            get { return year; }
            set { year = value; SetDateType(); }
        }
        private int month = DateTime.Now.Month;
        public int Month
        {
            get { return month; }
            set { month = value; SetDateType(); }
        }
        private Size cellRange;
        private UCDayPanel[,] days = new UCDayPanel[5, 3];
        private Label LBMonth, LBYear;
        #endregion

        // Initialize ==========================================================================
        #region Initialize
        public UCMonthL()
        {
            InitializeData();
            InitializeComponent();
            InitializeDesign();
        }
        private void InitializeDesign()
        {
            BackColor = Color.White;
        }
        private void InitializeData()
        {
            for (int row = 0; row < 5; row++)
            {
                for (int col = 0; col < 3; col++)
                {
                    days[row, col] = new UCDayPanel();
                    this.Controls.Add(days[row, col]);
                }
            }
            LBMonth = new Label();
            LBMonth.Font = new Font(LBMonth.Font.FontFamily, 42, FontStyle.Bold);
            LBMonth.ForeColor = Color.FromArgb(100, 100, 100);
            LBMonth.BorderStyle = BorderStyle.None;
            LBMonth.AutoSize = true;
            this.Controls.Add(LBMonth);
            LBYear = new Label();
            LBYear.Font = new Font(LBYear.Font.FontFamily, 15, FontStyle.Bold);
            LBYear.ForeColor = Color.FromArgb(102, 137, 206);
            LBYear.BorderStyle = BorderStyle.None;
            LBYear.AutoSize = true;
            this.Controls.Add(LBYear);
        }
        #endregion

        // OnResize ============================================================================
        #region OnResize
        private void UCMonthL_Resize(object sender, EventArgs e)
        {
            cellRange = new Size((int)Math.Floor((double)(Width - 6) / 4), (int)Math.Floor((double)(Height - 90) / 5));

            for (int row = 0; row < 5; row++)
            {
                for (int col = 0; col < 3; col++)
                {
                    days[row,col].Left = Width - (cellRange.Width + 1) * (3 - col) - 1;
                    days[row,col].Top = 78 + (cellRange.Height + 1) * row;
                    days[row,col].Size = cellRange;
                }
            }

            ResizeYearMonth(sender, e);
            ResizeNavigationButton(sender, e);
        }
        private void ResizeYearMonth(object sender, EventArgs e)
        {
            Point p = new Point((cellRange.Width - LBMonth.Width) / 2, 72);
            LBMonth.Location = p;
            p = new Point((cellRange.Width - LBYear.Width) / 2, 141);
            LBYear.Location = p;
        }
        private void ResizeNavigationButton(object sender, EventArgs e)
        {
            Point p = new Point(Width - 28, 20);
            PBFront2.Location = p;
            p.Offset(-30, 0);
            PBFront.Location = p;
            p.Offset(-30, 0);
            PBNow.Location = p;
            p.Offset(-30, 0);
            PBBack.Location = p;
            p.Offset(-30, 0);
            PBBack2.Location = p;
        }
        #endregion

        // Paint ===============================================================================
        #region Paint
        private void UCMonthL_Paint(object sender, PaintEventArgs e)
        {
            UCPage.PaintHeaderLine(sender, e, new Size(Width, Height));
            PaintCalendarLines(sender, e);
        }
        private void PaintCalendarLines(object sender, PaintEventArgs e)
        {
            Pen pen = new Pen(Global.GetColor(Global.MonthPageCalendarBorderColor[0]));
            Point p1 = new Point(Width - cellRange.Width * 3 - 6, 57);
            Point p2 = new Point(Width - 1, 57);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(0, 20);
            p2.Offset(0, 20);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(0, cellRange.Height + 1);
            p2.Offset(0, cellRange.Height + 1);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(0, cellRange.Height + 1);
            p2.Offset(0, cellRange.Height + 1);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(0, cellRange.Height + 1);
            p2.Offset(0, cellRange.Height + 1);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(0, cellRange.Height + 1);
            p2.Offset(0, cellRange.Height + 1);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(0, cellRange.Height + 1);
            p2.Offset(0, cellRange.Height + 1);
            e.Graphics.DrawLine(pen, p1, p2);
            p1 = new Point(Width - cellRange.Width * 3 - 6, 57);
            p2 = new Point(p1.X, p1.Y + 26 + cellRange.Height * 5);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(cellRange.Width + 2, 20);
            p2.Offset(cellRange.Width + 2, 0);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(cellRange.Width + 1, 0);
            p2.Offset(cellRange.Width + 1, 0);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(cellRange.Width + 1, 0);
            p2.Offset(cellRange.Width + 1, 0);
            e.Graphics.DrawLine(pen, p1, p2);

            pen = new Pen(Global.GetColor(Global.MonthPageCalendarBorderColor[1]));
            p1 = new Point(Width - cellRange.Width * 3 - 5, 76);
            p2 = new Point(Width - 1, 76);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(0, cellRange.Height * 5 + 7);
            p2.Offset(0, cellRange.Height * 5 + 7);
            e.Graphics.DrawLine(pen, p1, p2);
            p1 = new Point(Width - cellRange.Width * 3 - 5, 76);
            p2 = new Point(p1.X, p1.Y + cellRange.Height * 5 + 6);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(cellRange.Width * 3 + 4, -19);
            p2.Offset(cellRange.Width * 3 + 4, 0);
            e.Graphics.DrawLine(pen, p1, p2);

            SolidBrush b = new SolidBrush(Global.GetColor(Global.MonthPageCalendarBackColor));
            p1 = new Point(Width - cellRange.Width * 3 - 5, 58);
            Size s = new Size(cellRange.Width * 3 + 4, 18);
            e.Graphics.FillRectangle(b, new Rectangle(p1, s));
        }
        #endregion

        // Business Logic ======================================================================
        #region Business Logic
        private void UCMonthL_Load(object sender, EventArgs e)
        {
            Year = DateTime.Now.Year;
            Month = DateTime.Now.Month;
        }
        private void SetDateType()
        {
            LBYear.Text = Year.ToString();
            LBMonth.Text = Month.ToString();
            for (int row = 0; row < 5; row++)
            {
                for (int col = 0; col < 3; col++)
                {
                    days[row, col].Date = Calendar.GetDateTime(Year, Month, row, col);

                    if (days[row, col].Date.Month == Month)
                        days[row, col].IsCurrentMonth = true;
                    else
                        days[row, col].IsCurrentMonth = false;
                }
            }
            ResizeYearMonth(this, EventArgs.Empty);
        }

        public delegate void MonthEventHandler(object sender, EventArgs e, int year, int month);
        public event MonthEventHandler OnMonthChange;
        protected virtual void OnMonthChangeEvent(EventArgs e)
        {
            OnMonthChange(this, e, Year, Month);
        }

        private void PBBack2_Click(object sender, EventArgs e)
        {
            Year -= 1;
            OnMonthChangeEvent(EventArgs.Empty);
        }
        private void PBBack_Click(object sender, EventArgs e)
        {
            if (Month == 1)
            {
                Year -= 1;
                Month = 12;
            }
            else
            {
                Month -= 1;
            }
            OnMonthChangeEvent(EventArgs.Empty);
        }
        private void PBNow_Click(object sender, EventArgs e)
        {
            Year = DateTime.Now.Year;
            Month = DateTime.Now.Month;
            OnMonthChangeEvent(EventArgs.Empty);
        }
        private void PBFront_Click(object sender, EventArgs e)
        {
            if (Month == 12)
            {
                Year += 1;
                Month = 1;
            }
            else
            {
                Month += 1;
            }
            OnMonthChangeEvent(EventArgs.Empty);
        }
        private void PBFront2_Click(object sender, EventArgs e)
        {
            Year += 1;
            OnMonthChangeEvent(EventArgs.Empty);
        }
        #endregion
    }
}
